package com.ahao.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.ahao.abstraction.ISDKListener;
import com.ahao.abstraction.PayParams;
import com.ahao.abstraction.RoleInfoParams;
import com.ahao.sdk.model.Config;
import com.ahao.sdk.utils.ConfigBuilder;
import com.ahao.sdk.utils.ConfigUtils;
import com.ahao.sdk.utils.LogUtils;

import java.util.Properties;

public class AHAOPlatformSDKManager {
    private static AHAOPlatformSDKManager instance = new AHAOPlatformSDKManager();
    private ISDKListener mISDKListener;
    private Config localConfig;
    private boolean isPluginRegister = false;
    private PayParams mPayParams;

    public static AHAOPlatformSDKManager getInstance() {
        return instance;
    }

    public void login(Context context) {
        AHAOPlatformUiManager.getInstance().showLogin(context);
    }

    public void pay(Context context, PayParams payParams) {
        mPayParams = payParams;
        AHAOPlatformUiManager.getInstance().showPay(context);
    }

    public void init(Activity activity, ISDKListener isdkListener) {
        mISDKListener = isdkListener;
        AHAOPluginManager.getInstance().onCreate(activity);
    }


    public void initApplication(Application application) {
        // 防止多次初始化注册
        if (!isPluginRegister) {
            // 注册插件
            isPluginRegister = true;
            LogUtils.error("开始注册插件");
            try {
                String[] plugins = getLocalConfig(application).getPlugin_class_names().split(",");
                for (String pluginClassName : plugins) {
                    LogUtils.error(String.format("注册插件：%s", pluginClassName));
                    AHAOPluginManager.getInstance().registerPlugin(pluginClassName);
                }
                // 注册完之后调用插件的application初始化
                AHAOPluginManager.getInstance().initApplication(application);
            } catch (Exception e) {
                LogUtils.error("注册插件发生异常:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    public void uploadRoleInfo(Context context, RoleInfoParams roleInfoParams) {

    }

    public void onStart() {
        AHAOPluginManager.getInstance().onStart();
    }

    public void onPause() {
        AHAOPluginManager.getInstance().onPause();
    }

    public void onResume() {
        AHAOPluginManager.getInstance().onResume();
    }

    public void onNewIntent(Intent intent) {
        AHAOPluginManager.getInstance().onNewIntent(intent);
    }

    public void onStop() {
        AHAOPluginManager.getInstance().onStop();
    }

    public void onDestroy() {
        AHAOPluginManager.getInstance().onDestroy();
    }

    public void onRestart() {
        AHAOPluginManager.getInstance().onRestart();
    }

    public void onConfigurationChanged(Configuration newConfig) {
        AHAOPluginManager.getInstance().onConfigurationChanged(newConfig);
    }

    public void onSaveInstanceState(Bundle outState) {
        AHAOPluginManager.getInstance().onSaveInstanceState(outState);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        AHAOPluginManager.getInstance().onActivityResult(requestCode, resultCode, data);
    }

    public void onBackPressed() {
        AHAOPluginManager.getInstance().onBackPressed();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (AHAOPluginManager.getInstance().onKeyDown(keyCode, event)) {
            return true;
        }
        return false;
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        AHAOPluginManager.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public ISDKListener getSDKListener() {
        return mISDKListener;
    }

    public void emit(Context context, String type, String data) {
        AHAOPluginManager.getInstance().onEmit(context, type, data);
    }

    public Config getLocalConfig(Context context) {
        if (localConfig == null) {
            // 读取配置文件
            Properties properties = ConfigUtils.loadConfigFromAssets(context);
            localConfig = ConfigBuilder.buildConfig(Config.class, properties);
        }
        return localConfig;
    }

    public PayParams getPayParams() {
        return mPayParams;
    }
}
